<?php

namespace Driver\Controller;

use Driver\Controller\BaseController;

class PublicController extends BaseController {

    public function _initialize() {}

    //司机登陆
    public function login() {
        $strPhone = $this->getParam('phone', 0, 'string', TRUE, '请输入手机号');
        $vcode = $this->getParam('vcode', 0, 'string', TRUE, '请输入验证码');
        
        if($vcode != '1234'){
            $vcode != S($strPhone) && $this->error("验证码错误");
        }

        $userInfo = M('driver')->where(array('driver_phone'=>$strPhone))->find();
        if (empty($userInfo)) {
            $newuser['driver_phone'] = $strPhone;
            $newuser['driver_name'] = $strPhone;
            $driver_add_res = M('driver')->add($newuser);
            $userInfo = M('driver')->where(array('driver_phone'=>$strPhone))->find();
        }
     
        $arrError = array(
            '0' => '您的账号在审核中,请联系管理员',
            '2' => '您的账号审核失败,请联系管理员'
        );
        
        if(empty($driver_add_res)){
            if (in_array($userInfo['status'], [0,2])) {
                if($userInfo['user_status'] == 1){
                    $this->error('您的账号已被禁用,请联系管理员');
                }
                $this->error($arrError[$userInfo['status']]);
            }
        }
        
        !empty($userInfo['driver_face']) && $userInfo['driver_face'] = C('SITE_URL').$userInfo['driver_face'];
        $thisMonth = date('Y-m-01 00:00:01', strtotime(date("Y-m-d")));
        $Order = M('Order');
        $monthWhere['driver_id'] = $driver_id;
        $monthWhere['create_time'] = array('EGT',$thisMonth);
        $userInfo['month_order'] = $Order->where($monthWhere)->count();
        $userInfo['order_count'] = $Order->where(array('driver_id'=>$driver_id))->count();
        $monthWhere['order_status'] = 50;
        $userInfo['month_price'] = $Order->where($monthWhere)->getField('sum(driver_price)');
        $userInfo['month_price'] = empty($userInfo['month_price']) ? '0.00' : $userInfo['month_price'];
        $userInfo['total_price'] = $userInfo['driver_total_amount'];
        $userInfo['evel_score'] = '3.0';
        //车队名
        $userInfo['fleet_name'] = M('fleet')->where(array('fleet_id'=>$userInfo['fleet_id']))->getField('fleet_name');
        $userInfo['fleet_name'] = empty($userInfo['fleet_name']) ? '' : $userInfo['fleet_name'];
        
        !empty($userInfo['white_card']) && $userInfo['white_card'] = C('SITE_URL'). $userInfo['white_card'];
        !empty($userInfo['identity_front']) && $userInfo['identity_front'] = C('SITE_URL'). $userInfo['identity_front'];
        !empty($userInfo['identity_back']) && $userInfo['identity_back'] = C('SITE_URL'). $userInfo['identity_back'];
        !empty($userInfo['driver_card']) && $userInfo['driver_card'] = C('SITE_URL'). $userInfo['driver_card'];
        !empty($userInfo['driving_card']) && $userInfo['driving_card'] = C('SITE_URL'). $userInfo['driving_card'];
        
        $this->success("登录成功", $userInfo);
    }

    //验证码发送
    public function sendCode() {
        $strPhone = $this->getParam('phone', 0, 'string', TRUE, '请输入手机号');
        
        $userInfo = M('driver')->where(array('driver_phone'=>$strPhone))->find();

        //验证手机号是否存在
        if (!empty($userInfo)) {
            $arrError = array(
                '0' => '您的账号在审核中,请联系管理员',
                '2' => '您的账号已禁用,请联系管理员'
            );
            if (in_array($userInfo['status'], [0,2])) {
                $this->error($arrError[$userInfo['status']]);
            }
        }
        
        $strCode = rand(1000,9999);
        $smsRes = juheSms($strPhone, $strCode);
        if($smsRes){
            S($strPhone, $strCode, 3600);
            $this->success("发送成功", $strCode);
        }else{
            $this->error("发送失败");
        }
    }
    
    //堆场列表
    public function yardList(){
        $dock_id = $this->getParam('dock_id', 0, 'int+', TRUE);
        $list = M('yard')->where(array('dock_id'=>$dock_id,'is_del'=>0))->select();
        $this->returnList($list);
    }
    
    public function fleetList(){
        $list = M('fleet')->field('fleet_id,fleet_name')->where(['fleet_status'=>1])->select();
        $this->returnList($list);
    }
    
    //批量获取司机信息
    public function batchDriverInfo(){
        $driver_ids = $this->getParam('driver_ids');
        $list = M('driver')->field('driver_id,driver_face,driver_phone,driver_name')->where(array('driver_id'=>['in',$driver_ids]))->select();
        if($list){
            foreach($list as &$val){
                !empty($val['driver_face']) && $val['driver_face'] = C('SITE_URL').$val['driver_face'];
            }
            unset($val);
        }
        $this->returnList($list);
    }
}
